package controller

import (
	"github.com/gin-gonic/gin"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/schema"
	"net/http"
)

type UserController struct {
}

// Test
// @Description:
//type Test struct {
//	ID   uint
//	List string
//	Age  int
//}

func GetList(c *gin.Context) {
	dsn := "root:root@tcp(127.0.0.1:3306)/tp6?charset=utf8&parseTime=True&loc=Local"
	db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{
		NamingStrategy: schema.NamingStrategy{
			SingularTable: true, // 使用单数表名
		},
	})

	if err != nil {
		// 处理连接数据库错误
		return
	}

	// 查询单个记录
	//var test Test
	//if err := db.First(&test).Error; err != nil {
	//	// 处理查询错误
	//	return
	//}
	//fmt.Println(user)

	// 查询多个记录
	var tests []Test
	if err := db.Find(&tests).Error; err != nil {
		// 处理查询错误
		return
	}

	c.HTML(http.StatusOK, "index2.html", gin.H{
		"title": tests,
	})
	//fmt.Println(test)
	//ReturnSuccess(c, 0, "success", test, 1)
}

func GetUserInfo(c *gin.Context) {
	id := c.Param("id") //get接收参数
	ReturnSuccess(c, 0, "success", id, 1)
}

func PostUserInfo(c *gin.Context) {
	id := c.PostForm("id")
	name := c.PostForm("name") //post接收表单提交参数
	ReturnSuccess(c, 0, id, name, 1)
}

func PutUserInfo(c *gin.Context) {
	//put接收json格式参数，对比上面的PostUserInfo接收表单参数，这里与put无关
	param := make(map[string]interface{})
	err := c.BindJSON(&param)
	if err == nil {
		ReturnSuccess(c, 0, "success", param, 1)
		return
	}
	ReturnError(c, 4001, "error", gin.H{"err": err})

}
